import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimSun'] # 修改字体为宋体
ratings = pd.read_csv("./data/ratings.csv",sep=',',names=["user","movie_id","rating"])
movies = pd.read_csv("./data/movies.csv",sep=',',names=["movie_id","movie_name"])

print("13-2查看评分数据的结构")
print(ratings.head()) # 13-2查看评分数据的结构
print("\n")

ratings_gb_user = ratings.groupby('user').count().sort_values(by='movie_id', ascending=False)
print("13-3查看用户评论情况")
print(ratings_gb_user.head()) # 13-3查看用户评论情况
print("\n")

print("13-4查看用户评分统计信息")
print(ratings_gb_user.describe()) # 13-4查看用户评分统计信息
print("\n")

ratings_gb_user.movie_id.hist(bins=50) # 13-5查看用户评分直方图
plt.title('查看用户评分直方图')
plt.show()

ratings_gb_user.movie_id.hist(bins=50,range=(1, 10)) # 13-6查看小范围的用户评分直方图
plt.title('查看小范围的用户评分直方图')
plt.show()

user_rating = ratings.groupby('user').mean().sort_values(by='rating', ascending=False)

print("13-7查看评分值的分布情况")
print(user_rating.rating.describe()) # 13-7查看评分值的分布情况
print("\n")

ratings_gb_user = user_rating.rename(columns={'movie_id_x':'movie_id','rating_y':'rating'})
print("ratings_gb_user.head()")
print(ratings_gb_user.head(20))
print("\n")
fig, ax = plt.subplots()
ratings_gb_user.plot(x='movie_id', y='rating', kind='scatter', ax=ax, s=1) # 13-8查看评分值的分布散点图
plt.title('查看评分值的分布散点图')
plt.show()

ratings_gb_movie = ratings.groupby('movie_id').count().sort_values(by='user', ascending=False)
ratings_gb_movie = pd.merge(ratings_gb_movie,movies,on='movie_id',how='left')
print("13-9关联影片名称")
print(ratings_gb_movie.head()) # 13-9关联影片名称
print("\n")
print("ratings_gb_movie.user.describe()")
print(ratings_gb_movie.user.describe())
print("\n")
ratings_gb_user.movie_id.hist(bins=50) # 13-10查看电影数据的直方分布图
plt.title('查看电影数据的直方分布图')
plt.show()

movie_rating = ratings.groupby('movie_id')['rating'].mean().sort_values(ascending=False)
print("13-11查看电影数据的分布描述")
print(movie_rating.describe()) # 13-11查看电影数据的分布描述
print("\n")

ratings_gb_movie = pd.merge(ratings_gb_movie,movie_rating,on='movie_id',how='left')
print("ratings_gb_movie.head()")
print(ratings_gb_movie.head())
ratings_gb_movie.plot(x='user',y='rating_y',kind='scatter')
plt.title('查看电影数据的分布散点图')
plt.show()

